<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="/webjars/bootstrap/3.3.7/css/bootstrap.css">
  <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
  <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.js"></script>
  <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
  <script src="/webjars/axios/0.19.0/dist/axios.js" type="text/javascript"></script>
</head>
<body>
  <div id="vuebox">
    <h1 class="container">图书信息</h1>
    <div class="container">
      <button type="button" @click="addBook">添加图书</button>
      <table class="table table-striped">
        <thead>
        <tr>
          <th>图书名称</th>
          <th>图书作者</th>
          <th>购买时间</th>
          <th>图书分类</th>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="item in booksLists">
          <td>{{item.bName}}</td>
          <td>{{item.bAuthor}}</td>
          <td>{{item.bTime}}</td>
          <td>
            <span v-if="item.bType==1">言情小说</span>
            <span v-if="item.bType==2">历史小说</span>
          </td>
          <td>
            <button type="button" class="btn btn-success" @click="deleBook(item.bId)">删除</button>
          </td>
        </tr>
        </tbody>
      </table>
      <!--模态框-->
      <div class="modal" id="mymodal">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button @click="hideModal" class="close" type="button">&times;</button>
              <div class="modal-title">添加图书信息</div>
            </div>
            <div class="modal-body">
              <form class="form-horizontal" role="form">
                <div class="form-group">
                  <label for="title" class="col-sm-2 control-label" >图书名称:</label>
                  <div class="col-sm-10">
                    <input v-model="newBook.bName" id="title" type="text"  class="form-control" placeholder="请输入图书名称">
                  </div>
                </div>
                <div class="form-group">
                  <label for="summary" class="col-sm-2 control-label">图书作者</label>
                  <div class="col-sm-10">
                    <input v-model="newBook.bAuthor" id="summary" type="text" class="form-control" placeholder="请输入图书摘要"/>
                  </div>
                </div>
                <div class="form-group">
                  <label for="uploaduser" class="col-sm-2 control-label">购买日期</label>
                  <div class="col-sm-10">
                    <input v-model="newBook.bTime" id="uploaduser" type="text" class="form-control" placeholder="请输入上传人">
                  </div>
                </div>
                <div class="form-group">
                  <label for="categoryId" class="col-sm-2 control-label">图书类别:</label>
                  <div class="col-sm-10">
                    <select id="categoryId" class="form-control" v-model="newBook.bType">
                      <option value="1">言情小说</option>
                      <option value="2">历史小说</option>
                    </select>
                  </div>
                </div>
              </form>
            </div>
            <div class="modal-footer">
              <button class="btn btn-default" @click="hideModal()">关闭</button>
              <button class="btn btn-primary" @click="saveBook">保存</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>
<script type="text/javascript">
        var vm=new Vue({
            el:"#vuebox",
            data:{
                booksLists:{},
                result:"",
                newBook:{
                    bName:"",bAuthor:"",bTime:"",bType:"1"
                }
            },
            methods:{
                deleBook:function (id) {
                    axios.get("/deleBook",{params:{id:id}}).then(res=>{
                        this.result=res.data;
                        if(this.result=="SUCCESS"){
                            alert("删除成功!")
                            axios.get("/seleBook").then(res=>{
                                this.booksLists=res.data;
                            })
                        }else{
                            alert("删除失败!");
                        }
                    })
                },
                addBook(){
                    $("#mymodal").modal('show');
                },
                hideModal:function () {
                    $("#mymodal").modal('hide');
                },
                saveBook:function () {
                    axios.post("/addBook",this.newBook).then(res=>{
                        this.result=res.data;
                        if(this.result=="SUCCESS"){
                            alert("添加成功")
                            this.hideModal();
                            axios.get("/seleBook").then(res=>{
                                this.booksLists=res.data;
                            })
                        }else {
                            alert("添加失败!")
                        }
                    })
                }
            },
            mounted(){
                axios.get("/seleBook").then(res=>{
                    this.booksLists=res.data;
                })
            }
    })
</script>